﻿$(document).ready(function() {
    setInterval("slide($('#J_slide_list'),22,500)", 1500);
    var J_prize = $("#J_prize");
    var J_n = J_prize.find(".slide").eq(0).find(".slide-list").children().length - 1;
    var r1 = Math.floor(Math.random() * J_n), r2 = Math.floor(Math.random() * J_n), r3 = Math.floor(Math.random() * J_n), r4 = Math.floor(Math.random() * J_n); ;
    J_prize.find(".slide").eq(0).attr("data-eq", r1).find(".slide-list").css({ top: -r1 * 265 });
    J_prize.find(".slide").eq(1).attr("data-eq", r2).find(".slide-list").css({ top: -r2 * 265 });
    J_prize.find(".slide").eq(2).attr("data-eq", r3).find(".slide-list").css({ top: -r3 * 265 });
    J_prize.find(".slide").eq(3).attr("data-eq", r4).find(".slide-list").css({ top: -r4 * 265 });
    var J_prize1 = setInterval("slide($('#J_prize').find('.slide').eq(0),265,500)", 5000);
    var J_prize2 = setInterval("slide($('#J_prize').find('.slide').eq(1),265,500)", 5000);
    var J_prize3 = setInterval("slide($('#J_prize').find('.slide').eq(2),265,500)", 5000);
    var J_prize4 = setInterval("slide($('#J_prize').find('.slide').eq(3),265,500)", 5000);
    //抽奖
    $("#J_draw").click(function() {
        //状态
        var J_loginstatus = $("#J_loginstatus").val();
        var show = $("#J_prize");
        //未登录
        if (J_loginstatus == "1") {
            var light_login = $("#J_light_sign");
            light_login.show();
            light_login.find(".light-bg").css("height", $(document).height());
            light_login.find(".light-box").css({ left: ($(window).width() - light_login.find(".light-box").width()) / 2, top: ($(window).height() - light_login.find(".light-box").height()) / 2 + $(document).scrollTop() });
            light_login.find(".light-iframe").css("height", $(document).height());
            return;
        };
        //通行证无抽奖资格
        if (J_loginstatus == "3") {
            lightshow(["attention", "对不起，您的通行证今日已经摇过一次了哦~~请明天再来！", "", -1]);
            return;
        };
        //ip无抽奖资格
        if (J_loginstatus == "4") {
            lightshow(["attention", "对不起，您的IP今日已经达到上限了哦~~请明天再来！", "", -2]);
            return;
        };
        //如果有中奖资格则继续
        //如果在抽奖中
        if ($(this).attr("data") == "draw") {
            return;
        };
        //开始抽奖
        $(this).attr("data", "draw");
        clearInterval(J_prize1);
        clearInterval(J_prize2);
        clearInterval(J_prize3);
        clearInterval(J_prize4);
        //速度随机滚动
        s1 = Math.random() * 200; s1 += s1 < 65 ? 26 : 0;
        s2 = Math.random() * 200; s2 += s2 < 52 ? 39 : 0;
        s3 = Math.random() * 200; s3 += s3 < 39 ? 52 : 0;
        s4 = Math.random() * 200; s4 += s4 < 26 ? 65 : 0;
        lt1 = setInterval("play($('#J_prize').find('.slide').eq(0),265," + s1 + ")", s1);
        lt2 = setInterval("play($('#J_prize').find('.slide').eq(1),265," + s2 + ")", s2);
        lt3 = setInterval("play($('#J_prize').find('.slide').eq(2),265," + s3 + ")", s3);
        lt4 = setInterval("play($('#J_prize').find('.slide').eq(3),265," + s4 + ")", s4);
        //ajax提交
        var url = $("#J_submitlucky").val() + "?r=" + Math.random();
        $.ajax({
            async: true,
            type: "get",
            url: url,
            dataType: "text",
            success: function(msg) {
                $("#J_draw").removeAttr("data");
                var data = eval("(" + msg + ")");
                var award = data.awardRes;
                setTimeout("clearInterval(lt1); clearInterval(lt2); clearInterval(lt3); clearInterval(lt4);", 2000);
                setTimeout("lottery_award([\"" + award[0] + "\",\"" + award[1] + "\",\"" + award[2] + "\",\"" + award[3] + "\"])", 2500);
            },
            error: function() {
                $("#J_draw").removeAttr("data");
                /*停止滚动 不显示中奖*/
                setTimeout("clearInterval(lt1); clearInterval(lt2); clearInterval(lt3); clearInterval(lt4);", 2000);
                setTimeout("lottery_award([\"error\", \"抱歉，出错了！请刷新页面后再试！\", \"\",-3])", 2500);
                return;
            }
        });
    }).attr("href", "javascript:void(0)");
    //提示关闭
    $("#J_light .light-close, #J_light_sign .light-close").click(function() {
        $("#J_light, #J_light_sign").hide();
    });
    $("#J_light a.confirm").click(function() {
        $("#J_light").hide();
    }).attr("data", $("#J_light a.confirm").attr("href"));
});
//轮播
function slide(element, step, time) {
    var slide_list = element.find(".slide-list");
    var slide_triggers = element.find(".slide-triggers");
    var count = slide_list.children().length;
    if ((count < 2) || (slide_list.height() < element.height() + step)) { return; }
    var slide_list_first = slide_list.children().eq(0);
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = -1; element.attr("data-eq", "0"); };
    eq += 1;
    if (eq > count - 1) {
        eq = 0;
        slide_list.animate({ top: -count * step }, time, function() {
            slide_list.animate({ top: 0 }, 0);
            slide_list.children().removeAttr("style");
        });
    } else {
        slide_list.animate({ top: -eq * step }, time);
    }
    if (slide_list.height() - count * step < element.height()) {
        for (var i = 0; i < eq - 1; i++) {
            slide_list.children().eq(i).css({ "position": "relative", "top": (count * step) + "px" });
        };
    };
    element.attr("data-eq", eq);
    slide_triggers.children().removeClass("slide-current").eq(eq).addClass("slide-current");
};
//滚动
function play(element, step, speed) {
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = 0; element.find(".slide-list").animate({ top: 0 }, 0); element.find("li:first").removeAttr("style"); };
    var count = element.find(".slide-list").children().length;
    eq += 1;
    if (eq > count - 1) {
        element.find("li:first").css({ "position": "relative", "top": (count * step) + "px" });
        element.find(".slide-list").animate({ top: -count * step }, speed, function() {
            element.find(".slide-list").animate({ top: 0 }, 0);
            element.find("li:first").removeAttr("style");
        });
        eq = 0;
        element.attr("data-eq", eq);
        return;
    }
    element.find(".slide-list").animate({ top: -eq * step }, speed);
    element.attr("data-eq", eq);
    return;
};
//滚动结束处理
function play_end(element, step, speed, top) {
    var eq = parseInt(element.attr("data-eq"));
    var count = element.find(".slide-list").children().length;
    if (eq > top) {
        element.find("li:first").css({ "position": "relative", "top": (count * step) + "px" });
        element.find(".slide-list").animate({ top: -count * step }, speed, function() {
            element.find(".slide-list").animate({ top: 0 }, 0);
            element.find("li:first").removeAttr("style");
            element.attr("data-eq", awards).find(".slide-list").animate({ top: -265 * awards }, speed ).children().removeAttr("style");
        });        
        return;
    };
    element.attr("data-eq", awards).find(".slide-list").animate({ top: -265 * awards }, speed * 2).children().removeAttr("style");
    return;
};
//显示弹层
function lightshow(awardres) {
    $("#J_draw").removeAttr("data");
    //弹层处理
    var light = $("#J_light");
    light.show();
    light.find(".light-bg").css("height", $(document).height());
    light.find(".light-box").css({ left: ($(window).width() - light.find(".light-box").width()) / 2, top: ($(window).height() - light.find(".light-box").height()) / 2 + $(document).scrollTop() });
    light.find(".light-iframe").css("height", $(document).height());
    light.find(".message").removeClass("attention success error");
    light.find(".message").addClass(awardres[0]).find("dl").addClass(awardres[0]).find("dt").html(awardres[1]).next("dd").html(awardres[2]);
};
//中奖处理
function lottery_award(awardres) {
    var award = awardres[3];
    var show = $("#J_prize");
    var J_n = show.find(".slide").eq(0).find(".slide-list").children().length - 1;
    if (award > 0) {
        var href = (award == 9) ? "javascript:void(0)" : $("#J_light a.confirm").attr("data");
        $("#J_light a.confirm").attr("href", href);
        awards = award-1;
        play_end(show.find(".slide").eq(0), 265, 100, awards);
        play_end(show.find(".slide").eq(1), 265, 100, awards);
        play_end(show.find(".slide").eq(2), 265, 100, awards);
        play_end(show.find(".slide").eq(3), 265, 100, awards);
        setTimeout("$('#J_prize').find('.slide').find('.slide-list').animate({ top: -265 * " + awards + " }, 200)", 200);
        setTimeout("$('#J_prize').find('.slide').find('.slide-list').animate({ top: -265 * " + awards + " }, 200)", 1000);
    } else {
        s1 = Math.random() * J_n; s1 = Math.ceil(s1);
        s2 = Math.random() * J_n; s2 = Math.ceil(s2); if (s2 == s1) { s2 += 1; }; if (s2 >= J_n) { s2 = 0; };
        s3 = Math.random() * J_n; s3 = Math.ceil(s3); if (s3 == s2) { s3 += 1; }; if (s3 >= J_n) { s3 = 0; };
        s4 = Math.random() * J_n; s4 = Math.ceil(s4); if (s4 == s3) { s4 += 1; }; if (s4 >= J_n) { s4 = 0; };
        t1 = show.find(".slide").eq(0);
        t2 = show.find(".slide").eq(1);
        t3 = show.find(".slide").eq(2);
        t4 = show.find(".slide").eq(3);
        //如果一样
        if ((t1.attr("data-eq") == t2.attr("data-eq")) && (t1.attr("data-eq") == t3.attr("data-eq")) && (t1.attr("data-eq") == t4.attr("data-eq"))) {
            t1.attr("data-eq", s1);
            t2.attr("data-eq", s2);
            t3.attr("data-eq", s3);
            t4.attr("data-eq", s4);
        }
        setTimeout("$('#J_prize').find('.slide-list').each(function() {  var t = $(this).parent().attr('data-eq') * -265; $(this).animate({top:t},500); })", 200);
        //show.find(".slide-list").each(function() {  var t = $(this).parent().attr("data-eq") * -265; $(this).css("top", t); })
    };
    setTimeout("lightshow([\"" + awardres[0] + "\",\"" + awardres[1] + "\",\"" + awardres[2] + "\",\"" + awardres[3] + "\"])", 1500);
};

//提示
var awardcontent = {
    "award-1": ["attention", "对不起，您的通行证今日已经摇过一次了哦~~请明天再来！", ""],
    "award-2": ["attention", "对不起，您的IP今日已经摇了1次奖励了哟~~请明天再来！", ""],
    "award-3": ["error", "抱歉，出错了！请刷新页面后再试。", ""],
    "award0": ["error", "很遗憾，今天您的运气不够，明天再来一定能中哦。", ""],
    "award1": ["success", "恭喜您获得5000点盛大点券1张","点券在1~3个工作日内打入您的通行证账户。"],
    "award2": ["success", "恭喜您获得1000点盛大点券1张", "点券在1~3个工作日内打入您的通行证账户。"],
    "award3": ["success", "恭喜您获得500点盛大点券1张", "点券在1~3个工作日内打入您的通行证账户。"],
    "award4": ["success", "恭喜您获得300点盛大点券1张", "点券在1~3个工作日内打入您的通行证账户。"],
    "award5": ["success", "恭喜您获得100点盛大点券1张", "点券在1~3个工作日内打入您的通行证账户。"],
    "award6": ["success", "恭喜您获得价值0.5元的抵用券1张", "只要您在生成购物达7元，就可直接抵扣现金。"],
    "award7": ["success", "恭喜您获得5元现金", "点券在1~3个工作日内打入您的盛大钱包账户。"],
    "award8": ["success", "恭喜您获得10元现金", "点券在1~3个工作日内打入您的盛大钱包账户。"],
    "award9": ["success", "您幸运了，可以再要一次哦！", ""]
};
//在线客服
var snda_os_webrefer_ufg = {
    gm: 208,
    triggers: {
        floatwin: ["010101", "010102"],
        extend: [{ triggerid: "J_olservice", source: "010103"}]
    },
    t: "盛大商城-首页",
    hidewidgetwindow: true
};
$("#J_olservice").attr("href", "javascript:void(0)");